package DB;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.processors.JsonBeanProcessor;

import org.lightcouch.CouchDbClient;

import com.google.gson.JsonObject;

public class ChatTableDB {
	private CouchDbClient dbClient;
	public static final String IDCOL = "_id";
	public static final String CONTENTCOL = "content";
	public static final String TIMECOL = "time";
	public static final String USERFK = "userid";
	public static final String GROUPFK = "groupid";
	public static final String CHAT = "chat";
	public static final String GROUPNAME = "groupname";

	public ChatTableDB() {
		dbClient = new CouchDbClient(CHAT, true, "http", "127.0.0.1", 5984,
				null, null);
	}

	public String createChat(String userID, String groupID,String content) {
		JsonObject object = new JsonObject();
		String id = String.valueOf(new Date().getTime() + new Date().getTime());
		String groupName = new GroupTableDB().selectGroupByID(groupID).get(GroupTableDB.NAMECOL).getAsString();
		object.addProperty(IDCOL, id);
		object.addProperty(CONTENTCOL, content);
		object.addProperty(TIMECOL, new Date().toString());
		object.addProperty(USERFK, userID);
		object.addProperty(GROUPFK, groupID);
		object.addProperty(GROUPNAME, groupName);
		dbClient.save(object);
		return "success";
	}

	public String deleteChat(String userID, String groupID) {
		ArrayList<JsonObject> allDocs = (ArrayList<JsonObject>) dbClient.view("_all_docs").includeDocs(true).query(JsonObject.class);
		for(JsonObject json : allDocs){
			if(json.get(USERFK).getAsString().equals(userID) && json.get(GROUPFK).getAsString().equals(groupID)){
				dbClient.remove(json);
			}
		}		
		return "success";
	}
	public String deleteChatByUser(String userID){
		ArrayList<JsonObject> allDocs = (ArrayList<JsonObject>) dbClient.view("_all_docs").includeDocs(true).query(JsonObject.class);
		for(JsonObject json : allDocs){
			if(json.get(USERFK).getAsString().equals(userID)){
				dbClient.remove(json);
			}
		}		
		return "success";
	}
	public String deleteChatByGroup(String groupID){
		ArrayList<JsonObject> allDocs = (ArrayList<JsonObject>) dbClient.view("_all_docs").includeDocs(true).query(JsonObject.class);
		for(JsonObject json : allDocs){
			if(json.get(GROUPFK).getAsString().equals(groupID)){
				dbClient.remove(json);
			}
		}		
		return "success";
	}
	public ArrayList<JsonObject> selectChats(String userID, String groupID) {
		ArrayList<JsonObject> result = new ArrayList<JsonObject>();
		ArrayList<JsonObject> allDocs = (ArrayList<JsonObject>) dbClient.view("_all_docs").includeDocs(true).query(JsonObject.class);
		for(JsonObject json : allDocs){
			if(json.get(GROUPFK).getAsString().equals(groupID) && json.get(USERFK).getAsString().equals(userID)){
				result.add(json);
			}
		}
		return result;
	}
	
	public ArrayList<JsonObject> selectChatsByGroup(String groupID){
		ArrayList<JsonObject> result = new ArrayList<JsonObject>();
		ArrayList<JsonObject> allDocs = (ArrayList<JsonObject>) dbClient.view("_all_docs").includeDocs(true).query(JsonObject.class);
		for(JsonObject json : allDocs){
			if(json.get(GROUPFK).getAsString().equals(groupID)){
				result.add(json);
			}
		}
		return result;
	}

}
